<?php

namespace app\common\model;

use think\Model;

class UserRole extends Model
{

    // 表名
    protected $name = 'user_role';
    
    // 追加属性
    protected $append = [
        'grade_text',
        'group_text',
        'gclass_text',
        'elective_text',
        'subject_text'
    ];

    public function getGradeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['grade_id'] ?? "");
        return db('school_grade')->where('id', $value)->value('name');
    }
    public function getGroupTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['group_id'] ?? "");
        return db('user_group')->where('id', $value)->value('name');
    }
    public function getGclassTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['gclass_ids'] ?? "");
        return db('school_class')->where('id', 'in', $value)->column('name');
    }
    public function getElectiveTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['elective_ids'] ?? "");
        return db('school_class')->where('id', 'in', $value)->column('name');
    }
    public function getSubjectTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['subject_id'] ?? "");
        return db('subject')->where('id', $value)->value('name');
    }

    /**
     * 建立和user表(主表)的关联
     * belongsTo的参数包括：:belongsTo('关联模型名','外键名','关联表主键名',['模型别名定义'],'join类型');
     */
    public function user()
    {
        return $this->belongsTo('User');
    }
}
